const Models = require("./../../models");
const md5 = require('md5')

class User{
    static async addUser(ctx){
        const req = ctx.request.body;
        const userInfo = await Models.users.findOne({
            where:{
                number: req.number
            }
        })
        if(userInfo){
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '用户已存在'
            }
        }else if (req.number && req.passWord){
            try{
                const data = await Models.users.create({
                    ...req,
                    passWord: md5(req.passWord),
                    createdAt: new Date(),
                    updatedAt: new Date()
                })
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 200,
                    msg: '用户注册成功'
                }
            }catch(err){
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 501,
                    msg: '用户注册失败'
                }
            }
        }else{
            ctx.response.status = 416;
            ctx.response.body = {
                code: 200,
                msg: '注册信息不完整'
            }
        }
    }

    static async userLogin(ctx){
        const info = ctx.request.body;
        if (info.passWord && info.number){
            const user = await Models.users.findOne({
                where: {
                    number: info.number
                }
            })
            const md5Pass = md5(info.passWord);

            if (!user){
                ctx.response.status = 416;
                ctx.response.body = {
                    code: 416,
                    msg: '账号不存在'
                }
            }
            else if (md5Pass === user.passWord){
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 200,
                    msg: '登录成功',
                    data: user
                }
            }else{
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 416,
                    msg: '密码不正确'
                }
            }
        }else{
            ctx.response.status = 416;
            ctx.response.body = {
                code: 416,
                msg: '请输入密码或者账号'
            }
        }
    }

    static async getUser(ctx){
        const info = ctx.request.body;
        if (info.number){
            const data = await Models.users.findOne({
                where: {
                    number: info.number
                }
            })
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '获取成功',
                data
            }
        }else{
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '账号不存在'
            }
        }
    }

    static async editUser(ctx){
        const editData = ctx.request.body;
        if (editData.number){
            try{
                await Models.users.update({...editData},{
                    where: {
                        number: editData.number
                    }
                })
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 200,
                    msg: '用户信息修改完毕'
                }
            }catch (e) {
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 501,
                    msg: '用户信息修改失败'
                }
            }
        }else{
            ctx.response.status = 416;
            ctx.response.body = {
                code: 200,
                msg: '用户修改信息不完整'
            }
        }
    }

    static async getUsers(ctx){
        try{
            const getData = await Models.users.findAll();
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '用户数据获取成功',
                data: getData
            }
        }catch (e) {
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '用户数据获取失败'
            }
        }
    }

    static async getUsedUsers(ctx){
        try{
            const getData = await Models.users.findAll({
                where: {
                    isUsed: false
                }
            });
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '用户数据获取成功',
                data: getData
            }
        }catch (e) {
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '用户数据获取失败'
            }
        }
    }

    static async deleteUser(ctx){
        const req = ctx.request.body;
        await Models.users.update({isUsed: !req.isUsed},{
            where:{
                number: req.number
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据请求成功'
        }
    }

    static async addPrice(ctx){
        const req = ctx.request.body;
        const price = await Models.users.findOne({
            where: {
                number: req.number
            }
        })
        await Models.users.update({
            money: Number(req.money) + Number(price.money)
        },{
            where: {
                number: req.number
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '充值成功'
        }
    }

    static async getMoney(ctx){
        const req = ctx.request.body;
        const price = await Models.users.findOne({
            where: {
                number: req.number
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '余额获取成功',
            data: {
                money: price.money
            }
        }
    }

    static async reducePrice(ctx){
        const req = ctx.request.body;
        const price = await Models.users.findOne({
            where: {
                number: req.number
            }
        })
        if(req.price > price.money){
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '余额不足'
            }
        }else{
            await Models.users.update({
                money: price.money - req.price
            },{
                where: {
                    number: req.number
                }
            })
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '租金提交成功'
            }
        }
    }

    /*static async userLogin(ctx){
        const req = ctx.request.body;
        const userInfo = await Models.users.findOne({
            where: {
                number: req.number
            }
        })
        if( !userInfo ){
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '账号不存在'
            }
        }else if (userInfo.passWord === md5(req.password)){
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '登录成功',
                data: userInfo
            }
        }else{
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '密码不正确'
            }
        }
    }*/
}

module.exports = User